package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import database.Conn;

public class InfoBooking {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblInfoBook";
	public boolean AddInfoBooking(int id_customer,int date_rg,int number,int price,int payment){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_customer,date_rg,price,number,payment) VALUES (?,?,?,?,?)");
			pstm.setInt(1,id_customer);
			pstm.setInt(2,date_rg);
			pstm.setInt(3,price);
			pstm.setInt(4,number);
			pstm.setInt(5,payment);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteInfoBooking(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateInfoBooking(int id,int id_customer,int date_rg,int number,int price,int payment){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET id_customer=?,date_rg=?,number=?,price=?,payment=?  WHERE id=?");
			pstm.setInt(1,id_customer);
			pstm.setInt(2,date_rg);
			pstm.setInt(3,number);
			pstm.setInt(4,price);
			pstm.setInt(4,payment);
			pstm.setInt(5,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.InfoBooking> GetAllInfoBooking(){
		connect= Conn.getConnect();
		ArrayList<entity.InfoBooking> list = new ArrayList<entity.InfoBooking>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.InfoBooking ifb = new entity.InfoBooking();
				ifb.setId(rs.getInt("id"));
				ifb.setId_customer(rs.getInt("id_customer"));
				ifb.setDate(rs.getInt("date_gr"));
				ifb.setNumber_room(rs.getInt("number"));
				ifb.setPrice(rs.getInt("price"));
				ifb.setPayment(rs.getInt("payment"));
				list.add(ifb);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetInfoBookingById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
	// Lay thong tin gui den controller
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
